﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    public class BattlePlayer:Entity
    {
        public string Name;
        public int Hp = 10000;
        public int Power = 5;
        public int MaxPower = 5;
        public int BrokenPower = 0;
        public bool HaveGetCardFromDeck=false;
        public Dictionary<CardPlace, CardGroup> AllCard=new Dictionary<CardPlace, CardGroup>();
        public CardGroup this[CardPlace cardPlace]
        {
            get
            {
                CardGroup cards = null;
                if (!AllCard.ContainsKey(cardPlace)) AllCard.Add(cardPlace, new CardGroup() { CardPlace = cardPlace});
                AllCard.TryGetValue(cardPlace,out cards);
                return cards;
            }
        }
        public async void DrawCards(FightModel fightModel,int Num)
        {
            if (this[CardPlace.Deck].Cards.Count < Num)//卡组没牌时不抽卡
            {
                BattleEndInfo battleEndInfo = new BattleEndInfo()
                {
                    battleEndType=BattleEndType.DrawOutCard
                };
                await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.BattleEnd, fightModel, battleEndInfo);
                return;
            }
            List<BattleCard> list = new List<BattleCard>();
            for (int i = 0; i < Num; i++)
            {
                list.Add(this[CardPlace.Deck].Cards[i]);
                this[CardPlace.Deck].Cards[i].IfRightSide = true;
            }
            this[CardPlace.Deck].MoveCards(this[CardPlace.Hand], list);
        }
        public async void ChangeHp(DamageInfo damageInfo)
        {
            if (damageInfo.DamageValue == 0) return;
            damageInfo.Reciver = this;
            await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.HpChange, FightModel.Single, damageInfo);
            Hp -= damageInfo.DamageValue;
            if (Hp < 0)
            {
                BattleEndInfo battleEndInfo = new BattleEndInfo()
                {
                    battleEndType=BattleEndType.HpLessThen0,                    
                };
                await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.BattleEnd, FightModel.Single, battleEndInfo);
            }
            if (Hp < 5000 && !HaveGetCardFromDeck)
            {
                await ChooseCardFromDeck();
                HaveGetCardFromDeck = true;
            }
        }
        public async void ChangePower(PowerInfo powerInfo)
        {
            if (powerInfo.Power == 0) return;
            await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.PowerChange, FightModel.Single, powerInfo);
            Power -= powerInfo.Power;
            if (Power < 0) Power = 0;
        }

        public int GetGorgeous()
        {
            if (this[CardPlace.SpellChain].Cards.Count <= 0) return 0;
            return this[CardPlace.SpellChain].Cards[this[CardPlace.SpellChain].Cards.Count - 1].GetComponent<SpellCardComponent>().Gorgeous;
        }
        public async Task ChooseCardFromDeck()
        {
            SearchInfo searchInfo = new SearchInfo()
            {
                MaxChooseNum = 1,
                Condition = (x) => { return x.Count == 1; },
                ShowCard = this[CardPlace.Deck].Cards,
                OnSure = async(x) =>
                {
                    this[CardPlace.Deck].MoveCards(this[CardPlace.Hand], x);
                    foreach (var a in x) a.IfRightSide = true;
                    this[CardPlace.Deck].Shuffle();
                    await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.FreshUI, FightModel.Single, null);
                },
            };
            await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.ShowWindow, FightModel.Single, WindowType.Search, searchInfo);
        }
        public void RecoverPower(int num)
        {
            if (MaxPower - BrokenPower - Power > num)
            {
                Power = MaxPower - BrokenPower;
            }
            else
            {
                Power = Power + num;
            }
        }
    }
}
